#!/usr/bin/bash
## ## 获取ovpn 的连接状态信息，status 一分钟刷新一次比较慢这里直接登录后台管理接口拉取数据#### 
function get_online_user()
{
(echo "Aa123456";echo "status";sleep 1;echo " ";echo "quit")|telnet 172.16.50.253 23000
(echo "Aa123456";echo "status";sleep 1;echo " ";echo "quit")|telnet 172.16.50.250 23000
}
get_online_user >/tmp/status-server.log 2>/dev/null 
## 通过ip地址获取地位置信息#############################################################
# vars app_key 在https://tianapi.com中注册中拿到,一天免费查询次数有限，注意要使用qj来处理数据
function whereis()
{
ip_address=$1
if ! [ -x "$(command -v jq)" ]; then
  echo 'The jq package is not installed，Please run dnf install jq to install it.'
  exit 1
fi
data=$(curl "https://mesh.if.iqiyi.com/aid/ip/info?version=1.1.1&ip=${ip_address}" -s) # -s for slient output
status=$(echo $data | jq '.code' -r)
if [[ $status == "0" ]]; then
  city=$(echo $data |jq '.data' -r| jq '.cityCN' -r)
  province=$(echo $data |jq '.data' -r| jq '.provinceCN' -r)
  country=$( echo $data |jq '.data' -r| jq '.countryCN' -r)
  countryCN=$( echo $data |jq '.data' -r| jq '.countyCN' -r)
  echo ${country},${province},${city},${countryCN}.
  exit 1
fi
}
# 过滤用户端连接信息##############################################################################
## 这里要以systemd启动ovpn 进程并修改文件/usr/lib/systemd/system/openvpn-server@.service 的值修改为--status-version 3
egrep -i CLIENT_LIST  /tmp/status-server.log |egrep "10.100|10.200" >/tmp/ovpn_online_user.txt
num=$(cat  /tmp/ovpn_online_user.txt |wc -l)
rm -rf /tmp/ovpn_online_user_filter.txt
for i in $(seq 1 $num) 
do
common_name=$(sed -n "${i}p" /tmp/ovpn_online_user.txt |awk '{print $2}')
real_address=$(sed -n "${i}p" /tmp/ovpn_online_user.txt |awk '{print $3}'|awk -F ':' '{print $1}')
virtual_address=$(sed -n "${i}p" /tmp/ovpn_online_user.txt |awk '{print $4}')
Bytes_Received=$(sed -n "${i}p" /tmp/ovpn_online_user.txt |awk '{print $5}')
Bytes_Sent=$(sed -n "${i}p" /tmp/ovpn_online_user.txt |awk '{print $6}')
online_time_tmp=$(sed -n "${i}p" /tmp/ovpn_online_user.txt |awk '{print $7,$8,$9,$10,$11}')
online_time=$(date -d "$online_time_tmp" +"%Y-%m-%d-%H:%M:%S")
#client_id=$(sed -n "${i}p" /tmp/ovpn_online_user.txt |awk '{print $14}')
#peer_id=$(sed -n "${i}p" /tmp/ovpn_online_user.txt |awk '{print $15}')
ip_location=$(whereis $real_address)
echo "$common_name $real_address $virtual_address $(($Bytes_Received/1024))/$(($Bytes_Sent/1024))(KB) $online_time $ip_location" >>/tmp/ovpn_online_user_filter.txt
done

# 获取系统下当前的用户数 ############################################################################
echo "Ovpn_Users Status Real_Address Virtual_Address Received/Sent(KB) Connected_Since Client_Location" >/tmp/ovpn_oline_user_time.txt
echo "------------ -------- --------------- --------------- ----------------- ------------------- ------------------------- " >>/tmp/ovpn_oline_user_time.txt
##  获取当前系统下签发的证书，通过证书名过滤出common name################################################
find  /etc/openvpn/cerdir/pki/issued/ -name *.crt -exec basename {} .crt \; |grep -v vpn-server >/tmp/user.txt

## 基于common name 创建一个模板#######################################################################
num1=$(cat /tmp/user.txt |wc -l)
for i in $(seq 1 $num1)
do
        str0=$(sed -n "${i}p" /tmp/user.txt)
        echo "$str0 offline none none none none none" >>/tmp/ovpn_oline_user_time.txt
done

# 根据common　name条件来替换内容#########################################################################
numa=$(cat /tmp/ovpn_online_user_filter.txt|wc -l)
for i in $(seq 1 $numa)
do
	tmp_name=$(sed -n "${i}p" /tmp/ovpn_online_user_filter.txt |awk '{print $1}')
	tmp_context=$(sed -n "${i}p" /tmp/ovpn_online_user_filter.txt|awk -F' ' '{print $2,$3,$4,$5,$6}')
	sed -i "s#^${tmp_name}.*#${tmp_name}\ online\ ${tmp_context}#g" /tmp/ovpn_oline_user_time.txt
done
# 格式化输出文件内容############################################################################################
awk -F' ' '{printf "%-14s %-10s %-16s %-16s %-18s %-20s %-20s\n" ,$1,$2,$3,$4,$5,$6,$7}' /tmp/ovpn_oline_user_time.txt

